from django.db import models
from BYD_ERP.utils.base_model import BaseModel2

# python ../../manage.py startapp warehouse_info

# 迁移基础：erp_system, basic_info, purchase_info, goods_info, sale_info
# 后期在OutStorageModel中增加了字段daily_product_task
# 后期在InStorageModel中增加了字段daily_product_task

# python ../../manage.py makemigrations warehouse_info
# python ../../manage.py migrate warehouse_info

# python ../../manage.py check

# 入库类型
in_storage_choice = (
    ('1', '采购原辅料实货入库'),
    ('2', '采购原辅料发票入库'),
    ('3', '生产成品入库'),
    ('4', '改商标入库'),
    ('5', '废品返工入库'),
    ('6', '委托退货单'),
    ('7', '生产用料退库')
)


# 入库单 模型类
class InStorageModel(BaseModel2):
    # invoice：发票、清单
    invoices_date = models.DateTimeField('单据日期')
    # receipt：单据、收据、收条
    number_code = models.CharField('单据编号，默认不让用户填写', max_length=40)
    invoice = models.CharField('发票号码', max_length=50, blank=True, null=True)
    remark = models.CharField('备注/摘要', max_length=512, blank=True, null=True)
    payment_method = models.CharField('结账方式：电汇、银承、现金', max_length=10, blank=True, null=True)
    discount = models.DecimalField('优惠率，最多精确到小数点后两位', max_digits=5, decimal_places=2, default=0, blank=True, null=True)
    discount_money = models.DecimalField('优惠金额，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                         null=True)
    last_amount = models.DecimalField('优惠后总金额，最多精确到小数点后两位', max_digits=13, decimal_places=2, default=0, blank=True,
                                      null=True)
    other_money = models.DecimalField('其他费用，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                      null=True)
    this_payment = models.DecimalField('本次付款，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                       null=True)
    this_debt = models.DecimalField('本次欠款（限于当前入库单），最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                    null=True)
    number_count = models.DecimalField('入库总数量（限于当前入库单），最多精确到小数点后两位', max_digits=10, decimal_places=2)
    status = models.CharField('采购入库单的状态，0：未审核，1：已审核，2：部分付款，3：付款完成（包含欠款）', max_length=1, default=0,
                              blank=True, null=True)

    operator_user = models.ForeignKey('erp_system.UserModel', related_name='operator_in_list',
                                      on_delete=models.SET_NULL, verbose_name='制单人/入库操作人员，不能修改', blank=True, null=True)

    # 增加一个冗余字段
    operator_user_name = models.CharField('制单人/入库人员的真实姓名', max_length=20, blank=True, null=True)
    check_user = models.ForeignKey('erp_system.UserModel', verbose_name='审核人员，不能修改', on_delete=models.SET_NULL,
                                   blank=True, null=True)

    # 增加一个冗余字段
    check_user_name = models.CharField('审核人员的真实姓名', max_length=20, blank=True, null=True)

    account = models.ForeignKey('basic_info.SettlementAccountModel', on_delete=models.SET_NULL, blank=True, null=True,
                                verbose_name='结算账户，审核之后不能改')
    supplier = models.ForeignKey('basic_info.SupplierModel', on_delete=models.SET_NULL, blank=True, null=True,
                                 verbose_name='供应商，审核之后不能改')

    # 增加一个冗余字段
    supplier_name = models.CharField('供应商名称', max_length=30, blank=True, null=True)

    purchase_order = models.ForeignKey('purchase_info.PurchaseOrderModel', on_delete=models.SET_NULL, blank=True, null=True,
                                       verbose_name='采购订单，审核之后不能改')
    attachment_list = models.CharField('附件的ID列表，字段的值为：1,2,3,4', max_length=20, blank=True, null=True)

    daily_product_task = models.ForeignKey('production_info.DailyProductTaskModel', related_name='daily_product_in_storage_list',
                                           blank=True, null=True, on_delete=models.SET_NULL, verbose_name='每日生产任务单')

    # 入库类型：
    ################################
    # 探索/延伸：对不同角色的人有不同的默认值
    incoming_category = models.CharField('入库类型', choices=in_storage_choice, max_length=1, default='1', blank=True,
                                         null=True)

    class Meta:
        db_table = 't_storage_in'
        verbose_name = '入库单表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 入库单中货品模型类
class InStorageItemModel(BaseModel2):
    # 这些也是冗余字段，减少查询的时候，表连接查询的次数
    name = models.CharField('货品名称', max_length=20)
    number_code = models.CharField('货品编号', max_length=28, blank=True, null=True)
    specification = models.CharField('货品件号/规格', max_length=50, blank=True, null=True)
    model_number = models.CharField('货品型号', max_length=50, blank=True, null=True)
    partner = models.CharField('合作方/厂家', max_length=50, blank=True, null=True)
    brand = models.CharField('品牌', max_length=50, blank=True, null=True)
    length = models.CharField('型号长度mm', max_length=50, blank=True, null=True)
    section = models.CharField('截面/断面', max_length=50, blank=True, null=True)  # 6PK/8PK/10PK/AV13/AV15/AV17
    units = models.ForeignKey('goods_info.UnitsModel', on_delete=models.SET_NULL, blank=True, null=True)  # 计量单位
    units_name = models.CharField('计量单位名称', max_length=50, blank=True, null=True)
    goods_category = models.ForeignKey('goods_info.GoodsCategoryModel', on_delete=models.SET_NULL, blank=True,
                                       null=True, verbose_name='货品类别')
    category_name = models.CharField('货品类别名称', max_length=50, blank=True, null=True)

    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    into_count = models.DecimalField('入库数量，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    into_price = models.DecimalField('入库单价，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                     null=True)
    into_money = models.DecimalField('入库金额，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                     null=True)

    purchase_in_storage = models.ForeignKey('InStorageModel', related_name='item_list', on_delete=models.CASCADE,
                                            verbose_name='（采购）入库单', blank=True, null=True)
    goods = models.ForeignKey('goods_info.GoodsModel', related_name='item_list', on_delete=models.SET_NULL,
                              verbose_name='货品', null=True)
    warehouse = models.ForeignKey('basic_info.WarehouseModel', related_name='item_list', on_delete=models.SET_NULL,
                                  verbose_name='入库的仓库', null=True)
    warehouse_name = models.CharField('仓库名字', max_length=50, null=True)

    class Meta:
        db_table = 't_storage_in_items'
        verbose_name = '入库单的项目表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name + ' ' + self.specification


# 出库类型
out_storage_choice = (
    ('1', '销售出库（委托发货单）'),
    ('2', '生产领料'),
    ('3', '成品领料单（质量、样品）'),
    ('4', '库存返工出库')
)


# 出库单 模型类
class OutStorageModel(BaseModel2):
    # invoice：发票、清单
    invoices_date = models.DateTimeField('单据日期')
    # receipt：单据、收据、收条
    number_code = models.CharField('单据编号，默认不让用户填写', max_length=40)
    invoice = models.CharField('发票号码', max_length=50, blank=True, null=True)
    remark = models.CharField('备注/摘要', max_length=512, blank=True, null=True)
    payment_method = models.CharField('结账方式：电汇、银承、现金', max_length=10, blank=True, null=True)
    closing_date = models.DateTimeField('结账日期', blank=True, null=True)  # 市场客户结账周期一个月
    is_invoices = models.BooleanField('是否开票', default=True, blank=True, null=True)  # True是开票，False是不开票
    discount = models.DecimalField('优惠率，最多精确到小数点后两位', max_digits=5, decimal_places=2, default=0, blank=True, null=True)
    discount_money = models.DecimalField('优惠金额，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                         null=True)
    last_amount = models.DecimalField('优惠后总金额，最多精确到小数点后两位', max_digits=13, decimal_places=2, default=0, blank=True,
                                      null=True)
    other_money = models.DecimalField('其他费用，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                      null=True)
    deduction_deposit = models.DecimalField('定金抵扣，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                            null=True)
    this_proceeds = models.DecimalField('本次收款，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                        null=True)
    this_debt = models.DecimalField('本次欠款（限于当前出库单），最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                    null=True)
    number_count = models.DecimalField('出库总数量（限于当前出库单），最多精确到小数点后两位', max_digits=10, decimal_places=2)
    status = models.CharField('出库单的状态，0：未审核，1：已审核，2：部分收款，3：收款完成（包含欠款）', max_length=1, default=0,
                              blank=True, null=True)

    operator_user = models.ForeignKey('erp_system.UserModel', related_name='operator_outbound_list',
                                      on_delete=models.SET_NULL, verbose_name='出库制单人，不能修改', blank=True, null=True)

    # 增加一个冗余字段
    operator_user_name = models.CharField('出库制单人的真实姓名', max_length=20, blank=True, null=True)

    sales_man = models.ForeignKey('erp_system.UserModel', related_name='sales_man_outbound_list', blank=True, null=True,
                                  on_delete=models.SET_NULL, verbose_name='发货业务代表（经办人），不能修改')
    # 增加一个冗余字段
    sales_man_name = models.CharField('发货业务代表（经办人）的真实姓名', max_length=20, blank=True, null=True)

    check_user_fir = models.ForeignKey('erp_system.UserModel', related_name='check_user_fir_outbound_list',
                                       verbose_name='出库单审核人员，不能修改', on_delete=models.SET_NULL, blank=True, null=True)

    # 增加一个冗余字段
    check_user_fir_name = models.CharField('审核人员的真实姓名', max_length=20, blank=True, null=True)
    check_user_sec = models.ForeignKey('erp_system.UserModel', related_name='check_user_sec_outbound_list',
                                       verbose_name='出库单审核人员，不能修改', on_delete=models.SET_NULL, blank=True, null=True)

    # 增加一个冗余字段
    check_user_sec_name = models.CharField('审核人员的真实姓名', max_length=20, blank=True, null=True)

    account = models.ForeignKey('basic_info.SettlementAccountModel', on_delete=models.SET_NULL,
                                verbose_name='结算账户，审核之后不能改', blank=True, null=True)
    customer = models.ForeignKey('basic_info.CustomerModel', verbose_name='顾客，审核之后不能改', on_delete=models.SET_NULL,
                                 blank=True, null=True)

    # 增加冗余字段
    customer_name = models.CharField('顾客的全称', max_length=30, blank=True, null=True)
    customer_abbreviation_name = models.CharField('顾客的简称', max_length=10, blank=True, null=True)

    sale_order = models.ForeignKey('sale_info.SaleOrderModel', verbose_name='销售订单，审核之后不能改', on_delete=models.SET_NULL,
                                   blank=True, null=True)

    daily_product_task = models.ForeignKey('production_info.DailyProductTaskModel', related_name='daily_product_out_storage_list',
                                           blank=True, null=True, on_delete=models.SET_NULL, verbose_name='每日生产任务单')

    attachment_list = models.CharField('附件的ID列表，字段的值为：1,2,3,4', max_length=20, blank=True, null=True)

    # 出库类型：
    ################################
    # 探索/延伸：对不同角色的人有不同的默认值
    outbound_category = models.CharField('出库类型', choices=out_storage_choice, max_length=1, default='1', blank=True,
                                         null=True)

    class Meta:
        db_table = 't_storage_out'
        verbose_name = '出库单表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 出库单中货品模型类
class OutStorageItemModel(BaseModel2):
    # 这些也是冗余字段，减少查询的时候，表连接查询的次数
    name = models.CharField('货品名称', max_length=20)
    number_code = models.CharField('货品编号', max_length=28, blank=True, null=True)
    specification = models.CharField('货品件号/规格', max_length=50, blank=True, null=True)
    model_number = models.CharField('货品型号', max_length=50, blank=True, null=True)
    partner = models.ForeignKey('goods_info.PartnerModel', related_name='partner_out_list', on_delete=models.SET_NULL,
                                blank=True, null=True, verbose_name='合作厂家')
    brand = models.CharField('品牌', max_length=50, blank=True, null=True)
    length = models.CharField('型号长度mm', max_length=50, blank=True, null=True)
    section = models.ForeignKey('goods_info.SectionModel', related_name='section_out_list', on_delete=models.SET_NULL,
                                blank=True, null=True, verbose_name='截面/断面')  # 6PK/8PK/10PK/AV13/AV15/AV17
    units = models.ForeignKey('goods_info.UnitsModel', on_delete=models.SET_NULL, blank=True, null=True,
                              verbose_name='计量单位')
    units_name = models.CharField('计量单位名称', max_length=50, blank=True, null=True)
    goods_category = models.ForeignKey('goods_info.GoodsCategoryModel', on_delete=models.SET_NULL, blank=True,
                                       null=True, verbose_name='货品类别')
    goods_category_name = models.CharField('货品类别名称', max_length=50, blank=True, null=True)

    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    out_count = models.DecimalField('出库数量，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    out_price = models.DecimalField('出库单价/销售单价，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                    null=True)
    out_money = models.DecimalField('出库金额，最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0, blank=True,
                                    null=True)

    out_storage = models.ForeignKey('OutStorageModel', related_name='item_list_out', on_delete=models.CASCADE,
                                    verbose_name='销售出库单', blank=True, null=True)
    goods = models.ForeignKey('goods_info.GoodsModel', related_name='item_list_goods_out', on_delete=models.SET_NULL,
                              verbose_name='货品', blank=True, null=True)
    warehouse = models.ForeignKey('basic_info.WarehouseModel', related_name='item_list_warehouse',
                                  on_delete=models.SET_NULL,
                                  verbose_name='出库的仓库', blank=True, null=True)
    warehouse_name = models.CharField('仓库名字', max_length=50, blank=True, null=True)

    class Meta:
        db_table = 't_storage_out_items'
        verbose_name = '出库单的项目表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name + ' ' + self.specification
